#define _CRT_SECURE_NO_WARNINGS
#include "Tree.h"
#include "Queue.h"
void preOrder(BTNode* root) {
	if (root == NULL) {
		printf("NULL ");
		return;
	}
	printf("%c ", root->data);
	preOrder(root->left);
	preOrder(root->right);
}

void InOrder(BTNode* root) {
	if (root == NULL) {
		printf("NULL ");
		return;
	}
	InOrder(root->left);
	printf("%c ", root->data);
	InOrder(root->right);
}

void postOrder(BTNode* root) {
	if (root == NULL) {
		printf("NULL ");
		return;
	}
	postOrder(root->left);
	postOrder(root->right);
	printf("%c ", root->data);
}

int BinaryTreeSize(BTNode* root) {
	if (!root)
		return 0;
	return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}

int BinaryTreeLeafSize(BTNode* root) {
	if (!root)
		return 0;
	if (root->left == NULL && root->right == NULL)
		return 1;
	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

int BinaryTreeLevelKSize(BTNode* root, int k) {
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}

int BinaryTreeDepth(BTNode* root) {
	if (root == NULL)
		return 0;
	int L = BinaryTreeDepth(root->left);
	int R = BinaryTreeDepth(root->right);
	return (L > R ? L : R) + 1;
}

BTNode* BinaryTreeFind(BTNode* root, BTDataType x) {
	if (root == NULL)
		return NULL;
	if (root->data == x)
		return root;
	BTNode* left = BinaryTreeFind(root->left, x);
	if (left) {
		return left;
	}
	BTNode* right = BinaryTreeFind(root->right, x);
	return right;
}

void BinaryTreeDestroy(BTNode** proot) {
	if (*proot == NULL)return;
	BinaryTreeDestroy(&((*proot)->left));
	BinaryTreeDestroy(&((*proot)->right));
	free(*proot);
	*proot = NULL;
}

//bfs of Tree... 

void LevelOrder(BTNode* root) {
	Queue q;
	QueueInit(&q);
	QueuePush(&q, root);
	while (!QueueEmpty(&q)) {
		BTNode* top = QueueFront(&q);
		QueuePop(&q);
		printf("%c ", top->data);
		if (top->left)QueuePush(&q, top->left);
		if (top->right)QueuePush(&q, top->right);
	}
	QueueDestroy(&q);
}

bool BinaryTreeComplete(BTNode* root) {
	Queue q;
	QueueInit(&q);
	QueuePush(&q, root);
	while (!QueueEmpty(&q)) {
		BTNode* top = QueueFront(&q);
		QueuePop(&q);
		if (top == NULL)break;
		QueuePush(&q, top->left);
		QueuePush(&q, top->right);

	}
	while (!QueueEmpty(&q)) {
		if (QueueFront(&q)) {
			QueueDestroy(&q);
			return false;
		}
	}
	QueueDestroy(&q);
	return true;
}